package scene

import (
	"container/list"
	"fmt"
	"time"
)

func MonsterStep(scene *GameScene) {

	monsterMap := scene.RunMonsterMap[monsterType]
	for _, monster := range monsterMap {
		roads := monster.Roads
		PersonRunning(roads, monster.Person)
		monster.OnPositionChange()
	}
}

func MonsteAiAction(scene *GameScene) {
	monsterMap := scene.MonsterMap[monsterType]
	for _, monster := range monsterMap {
		if monster.PersonAiCtl != nil {
			monster.PersonAiCtl.Update()
		}
	}
}

func PersonRunning(roads list.List, person *Person) {
	if roads.Len() > 0 {
		now := time.Now().Unix()
		interval := now - person.PreStepTime
		personPosition := person.Position
		speed := (int64)(100)
		dis := interval / speed
		dis64 := float64(dis)
		person.PreStepTime = now
		for e := roads.Front(); e != nil; {
			next := e.Next()
			nextP := next.Value.(Point)
			disNext := CountDistance(personPosition, nextP)
			if dis64 < disNext {
				CountPosition(personPosition, nextP, dis64)
			} else {
				val := roads.Remove(e)
				e = next
				rPoint := val.(Point)
				personPosition.X = rPoint.X
				personPosition.Y = rPoint.Y
				dis64 -= disNext
			}
		}
	} else {
		fmt.Println("monster rolads is empty id:", person.Id)
	}
}
